package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Experience;
import com.personal.portal.admin.service.ExperienceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作经历控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "工作经历", description = "工作经历信息管理接口")
@RestController
@RequestMapping("/admin/experience")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ExperienceController {

    @Resource
    private ExperienceService experienceService;

    /**
     * 根据ID查询工作经历
     */
    @Operation(summary = "根据ID查询工作经历", description = "根据工作经历ID查询详细信息")
    @GetMapping("/{id}")
    public Result<Experience> queryById(
            @Parameter(description = "工作经历ID", required = true) 
            @PathVariable Long id) {
        try {
            Experience experience = experienceService.queryById(id);
            if (experience != null) {
                return Result.success(experience);
            } else {
                return Result.error("工作经历信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询工作经历信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询工作经历列表
     */
    @Operation(summary = "根据条件查询工作经历列表", description = "根据公司、职位等条件查询工作经历信息")
    @PostMapping("/list")
    public Result<List<Experience>> queryList(
            @Parameter(description = "工作经历查询条件") 
            @RequestBody Experience experience) {
        try {
            List<Experience> experienceList = experienceService.queryList(experience);
            return Result.success(experienceList);
        } catch (Exception e) {
            return Result.error("查询工作经历列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询工作经历
     */
    @Operation(summary = "分页查询工作经历", description = "支持分页和条件查询的工作经历信息")
    @PostMapping("/page")
    public Result<Result.PageResult<Experience>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "工作经历查询条件") 
            @RequestBody Experience experience) {
        try {
            Result.PageResult<Experience> pageResult = experienceService.queryPage(pageNum, pageSize, experience);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询工作经历失败: " + e.getMessage());
        }
    }

    /**
     * 创建工作经历
     */
    @Operation(summary = "创建工作经历", description = "新增一条工作经历信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "工作经历信息", required = true) 
            @RequestBody Experience experience) {
        try {
            int result = experienceService.insert(experience);
            if (result > 0) {
                return Result.success("创建工作经历成功", experience.getId());
            } else {
                return Result.error("创建工作经历失败");
            }
        } catch (Exception e) {
            return Result.error("创建工作经历失败: " + e.getMessage());
        }
    }

    /**
     * 更新工作经历
     */
    @Operation(summary = "更新工作经历", description = "更新指定ID的工作经历信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "工作经历ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "工作经历信息", required = true) 
            @RequestBody Experience experience) {
        try {
            experience.setId(id);
            int result = experienceService.update(experience);
            if (result > 0) {
                return Result.success("更新工作经历成功", id);
            } else {
                return Result.error("更新工作经历失败");
            }
        } catch (Exception e) {
            return Result.error("更新工作经历失败: " + e.getMessage());
        }
    }

    /**
     * 删除工作经历
     */
    @Operation(summary = "删除工作经历", description = "根据ID删除指定的工作经历信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "工作经历ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = experienceService.deleteById(id);
            if (result > 0) {
                return Result.success("删除工作经历成功", id);
            } else {
                return Result.error("删除工作经历失败");
            }
        } catch (Exception e) {
            return Result.error("删除工作经历失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除工作经历
     */
    @Operation(summary = "批量删除工作经历", description = "根据ID列表批量删除工作经历信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "工作经历ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = experienceService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除工作经历失败: " + e.getMessage());
        }
    }
}